package youleche.com.controler.servlet;

import java.beans.IntrospectionException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import youleche.com.business.BSeller;
import youleche.com.business.BUser;
import youleche.com.controler.servlet.common.UlecheHttpServlet;
import youleche.com.model.MSeller;
import youleche.com.model.MUser;
import youleche.com.util.JsonHelper;

/**
 * Servlet implementation class SSellerServlet
 */
public class SSellerServlet extends UlecheHttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SSellerServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		int action = Integer.parseInt(request.getParameter("action"));
		switch(action){
			case 0 :	
				//根据用户名得到商家信息
				this.getSellerInfoByName(request, response);
				break;	
			case 1 :	
				//添加或者更新商家信息
				this.addOrUpdateSellerInfo(request, response);
				break;	
		}
	}
	
	/**
	 * 添加或者更新商家信息
	 * @param request
	 * @param response
	 */
	private void addOrUpdateSellerInfo(HttpServletRequest request, HttpServletResponse response){
		BSeller handler = new BSeller();
		try {
			MSeller seller = (MSeller)JsonHelper.getObjectByJsonString(request, MSeller.class);
			if(handler.getSellerByUserId(seller.getUserId()) == null){
				seller.setIsDelete("0");
				if(handler.addSellerInfo(seller) >0){
					super.response(response, "1");
					return;
				}
			}else{
				//更新
				if(handler.updateSellerInfoById(seller) >0){
					super.response(response, "1");
					return;
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			super.response(response, "0");
		} 
		
		super.response(response, "0");
	}
	
	/**
	 * 根据用户名得到商家信息
	 * @param request
	 * @param response
	 */
	private void getSellerInfoByName(HttpServletRequest request, HttpServletResponse response){
		String userName = request.getParameter("userName");
		BUser handler = new BUser();
		try {
			MUser user = handler.getUserByUserName(userName);
			BSeller sellHandler = new BSeller();
			MSeller seller = sellHandler.getSellerByUserId(user.getId());
			if(seller == null){
				super.response(response, "");
				return;
			}
			Gson gson = new Gson();
			super.response(response, gson.toJson(seller));
		} catch (Exception e) {
			e.printStackTrace();
			super.response(response, "");
		}
	}

}
